package com.liy.cloud.common.trade.utils;
/**
 * @author shitou
 * @Time：2018年8月13日 下午2:08:35
 * @version 1.0
 */
public class TradeMessageParser {

	private int[] splitNumber;
	private String[] arr;
	private String message;
	private String preValue;
	
	public TradeMessageParser(String message, int[] splitNumber) {
		this.splitNumber = splitNumber;
		this.message = message;
		this.splitMessage();
	};
	
	public TradeMessageParser(int[] splitNumber) {
		this.splitNumber = splitNumber;
	};
	
	private String[] splitMessage() {

		this.arr = new String[splitNumber.length];
		int start = 0;
		for (int i = 0; i < splitNumber.length; i++) {
			
			int length = splitNumber[i];	//截取长度
			if(length == -1) {	// -1为特定行业数据
				length = Integer.parseInt(this.preValue);	//上一个值为特定行业数据长度
			}
			
			String str = message.substring(start, start + length);
			this.arr[i] = str;
			this.preValue = str;
			
			start += splitNumber[i];	//开始序号
			System.out.println(i + "【" + splitNumber[i] + "】" +"	=	substring(" + start + ", " + (start + splitNumber[i]) + ")	=	" + str);
		}
		
		return arr;
	}
	
	
	public String getFiled(int index) {
		
		return this.arr[index];
		
//		int start = 0;
//		for (int i = 0; i < index; i++) {
//			start += splitNumber[i];
//		}
//		
//		int length = splitNumber[index];
//		if(length == -1) {	// -1为特定行业数据
//			length = Integer.parseInt(this.preValue);	//上一个值为特定行业数据长度
//		}
//		
//		String str = message.substring(start, start + length);
//		this.preValue = str;
//		System.out.println(index + "	=	substring(" + start + ", " + (start + splitNumber[index]) + ")	=	" + str);
//		return str;
	}

	public int[] getSplitNumber() {
		return splitNumber;
	}

	public void setSplitNumber(int[] splitNumber) {
		this.splitNumber = splitNumber;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
	
}
